#include "Environment.h"

#include <QImage>
#include <QBrush>
#include <QRadialGradient>
#include <QPainter>
#include <QColor>

/**
 * Generates an environment of RGB where:
 *
 * Red is toxins
 * Green is nutrients
 * Blue is viscosity
 */
void Environment::generateEnvironment()
{
    if (image != NULL)
    {
        delete image;
    }

    image = new QImage(width, height, QImage::Format_ARGB32);

    QPainter painter(image);

    painter.fillRect(0, 0, 800, 600, QBrush(QColor(0x00, 0x00, 0x00, 255)));
    painter.setCompositionMode(QPainter::CompositionMode_Plus);
    painter.setPen(Qt::NoPen);

    int i;

    // TOXINS
    for (i = 0; i < toxicCells; i++)
    {
        int x = int((double(qrand()) / double(RAND_MAX)) * 800.0);
        int y = int((double(qrand()) / double(RAND_MAX)) * 600.0);
        int radius = int((double(qrand()) / double(RAND_MAX)) * 150.0) + 50;
        int amount = int((double(qrand()) / double(RAND_MAX)) * (double)0x66) + 0x22;

        QRadialGradient toxicGradient(x, y, radius, x, y);
        toxicGradient.setColorAt(0, QColor(amount, 0, 0, 0xFF));
        toxicGradient.setColorAt(1, QColor(amount, 0, 0, 0));

        QBrush brush(toxicGradient);

        painter.setBrush(brush);

        painter.drawEllipse(QPoint(x,y), radius, radius);
    }

    // NUTRIENTS
    for (i = 0; i < nutrientCells; i++)
    {
        int x = int((double(qrand()) / double(RAND_MAX)) * 800.0);
        int y = int((double(qrand()) / double(RAND_MAX)) * 600.0);
        int radius = int((double(qrand()) / double(RAND_MAX)) * 150.0) + 50;
        int amount = int((double(qrand()) / double(RAND_MAX)) * (double)0x66) + 0x22;

        QRadialGradient nutrientGradient(x, y, radius, x, y);
        nutrientGradient.setColorAt(0, QColor(0, amount, 0, 0xFF));
        nutrientGradient.setColorAt(1, QColor(0, amount, 0, 0));

        QBrush brush(nutrientGradient);

        painter.setBrush(brush);

        painter.drawEllipse(QPoint(x,y), radius, radius);
    }

    // VISCOSITY
    for (i = 0; i < viscosityCells; i++)
    {
        int x = int((double(qrand()) / double(RAND_MAX)) * 800.0);
        int y = int((double(qrand()) / double(RAND_MAX)) * 600.0);
        int radius = int((double(qrand()) / double(RAND_MAX)) * 150.0) + 50;
        int amount = int((double(qrand()) / double(RAND_MAX)) * (double)0x66) + 0x22;

        QRadialGradient viscosityGradient(x, y, radius, x, y);
        viscosityGradient.setColorAt(0, QColor(0, 0, amount, 0xFF));
        viscosityGradient.setColorAt(1, QColor(0, 0, amount, 0));

        QBrush brush(viscosityGradient);

        painter.setBrush(brush);

        painter.drawEllipse(QPoint(x,y), radius, radius);
    }
}
